package com.wyp.common;

/**
 * 返回结果集封装-解决提问：前后端数据接口约定{
 * msg，code，data
 * }
 *
 * @author: Oliver
 */
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    private Result(T data) {
        this.data = data;
        this.code = 0;
    }

    private Result(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }

    private Result(Integer code, T data) {
        this.data = data;
        this.code = code;
    }

    /**
     * 成功时候调用
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(0, data);
    }

    /**
     * 失败时候调用
     *
     * @param codeMsg
     * @return
     */
    public static Result error(CodeMsg codeMsg) {
        return new Result(codeMsg);
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
